package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.BaseData;
import cn.zj.logistics.pojo.Customer;
import cn.zj.logistics.pojo.CustomerExample;
import cn.zj.logistics.pojo.CustomerExample.Criteria;
import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.pojo.UserExample;
import cn.zj.logistics.service.BaseDataService;
import cn.zj.logistics.service.CustomerService;
import cn.zj.logistics.service.UserService;
import cn.zj.logistics.util.Constant;

@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private UserService userService;
	
	
	@Autowired
	private BaseDataService baseDataService;
	

	@RequiresPermissions("customer:customerPage")
	@RequestMapping("/customerPage")
	public String customerPage() {

		return "customerPage";
	}
	
	@RequiresPermissions("customer:list")
	@RequestMapping("/list")
	@ResponseBody
	public <T> PageInfo<Customer> list(@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "10") int pageSize, String keyword) {

		PageHelper.startPage(pageNum, pageSize);

		CustomerExample example = new CustomerExample();
		
		Criteria criteria1 = example.createCriteria();
		/*
		 * 如果当前的登录的用户的角色是业务员时候，就只能查看自己的客户
		 */
		//1.获取身份
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		
		if(Constant.ROLE_SALESMAN.equals(user.getRolename())) {
			//只能查询出用户id是当前身份的数据
			criteria1.andUserIdEqualTo(user.getUserId());
		}
		

		// 前台传递关键字参数不为空的时候才参与条件查询
		if (StringUtils.isNotBlank(keyword)) {
			
			criteria1.andCustomerNameLike("%" + keyword + "%");
			
		}

		List<Customer> customers = customerService.selectByExample(example);

		PageInfo<Customer> pageInfo = new PageInfo<>(customers);

		return pageInfo;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long customerId) {

		int row = customerService.deleteByPrimaryKey(customerId);

		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}

	// 新增功能
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject insert(Customer customer) {

		int row = customerService.insert(customer);

		MessageObject mo = MessageObject.createMo(0, "新增数据数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "新增数据成功");
		}
		return mo;
	}

	// 新增功能
	@RequestMapping("/update")
	@ResponseBody
	public MessageObject update(Customer customer) {

		int row = customerService.updateByPrimaryKeySelective(customer);

		MessageObject mo = MessageObject.createMo(0, "修改数据数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "修改数据成功");
		}
		return mo;
	}

	@RequestMapping("/customerEdit")
	public String customerEdit(Model m, Long customerId) {


		if (customerId != null) {
			// 查询出当前要修改的用户信息共享给编辑页面以供修改数据回显
			Customer customer = customerService.selectByPrimaryKey(customerId);
			m.addAttribute("customer", customer);
		}
		
		/*
		 * 查询出用户表中的所有业务员
		 */
		UserExample example = new UserExample();
		cn.zj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRolenameEqualTo(Constant.ROLE_SALESMAN);
		List<User> users = userService.selectByExample(example );
		m.addAttribute("users", users);
		
		/*
		 * 查询出基础数据表的区间管理数据
		 */
		List<BaseData> intervals = baseDataService.selectBaseDatasByParentName(Constant.BASIC_COMMON_INTERVAL);
		m.addAttribute("intervals", intervals);
		
		
		return "customerEdit";
	}



}
